/*
 * Project Beknyou
 * Copyright (c) 2010-2011 Saint Paul College, All Rights Reserved
 * Redistributions in source code form must reproduce the above
 * Copyright and this condition.
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 */
/*
 * clientButtonForm.java
 *
 * Created on Sep 27, 2011, 9:16:27 PM
 */
package com.benkyou.client;

import com.benkyou.client.network.Chatter;
import com.benkyou.common.Player;
import com.benkyou.util.HTMLColorGenerator;
import com.benkyou.util.UserListModel;

/**
 *
 * @author Austin Allman
 */
public class clientButtonForm extends javax.swing.JPanel {
    private UserListModel userListModel = new UserListModel();
private StringBuilder chatMessage = new StringBuilder();
private HTMLColorGenerator colorGenerator = new HTMLColorGenerator();
private String oldMessage;
/**
 * 
 * @param name
 */
public void addToUserList(Chatter chatter){
    userListModel.addUser(chatter);
}
public void removeFromUserList(Chatter chatter){
    userListModel.removeElement(chatter);
}

    void setChatText(String name, String message) {
        if(!message.equals(oldMessage)){
            oldMessage = message;
        String chatBoxMessage = "<font color='"+ colorGenerator.getColorFromName(name) + "'><b>" + name + "</b></font> : " + message + "<br />";
        chatMessage.append(chatBoxMessage);
        String wrapper = "<html><body>" + chatMessage + "</body></html>";
        chatTextPane.setText(wrapper);
        // Set selection to the end so that the scroll panel will scroll
        // down.
        chatTextPane.select(chatMessage.length(), chatMessage.length());
        }
    }
    /**
     * 
     * @return
     */
    public String getChatMessage(){
        String returnString = getChatTextField().getText();
        getChatTextField().setText("");
        return returnString;        
    }

    /**
     * 
     * @return
     */
    public javax.swing.JButton getSendButton() {
        return sendButton;
    }

    /** Creates new form clientButtonForm */
    public clientButtonForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sendButton = new javax.swing.JButton();
        classListScrollPane = new javax.swing.JScrollPane();
        userJList = new javax.swing.JList();
        chatTextField = new javax.swing.JTextField();
        chatTextPaneScrollPane = new javax.swing.JScrollPane();
        chatTextPane = new javax.swing.JEditorPane();

        sendButton.setText("Send");

        userJList.setModel(userListModel);
        userJList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        userJList.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        classListScrollPane.setViewportView(userJList);
        userJList.setSelectedIndex(0);
        userListModel.addJListListener(userJList);
        userJList.addListSelectionListener(userListModel);

        chatTextPane.setContentType("text/html");
        chatTextPane.setEditable(false);
        chatTextPaneScrollPane.setViewportView(chatTextPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(chatTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sendButton))
                    .addComponent(chatTextPaneScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 471, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(classListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(chatTextPaneScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(chatTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sendButton))
                        .addGap(14, 14, 14))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(classListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField chatTextField;
    private javax.swing.JEditorPane chatTextPane;
    private javax.swing.JScrollPane chatTextPaneScrollPane;
    private javax.swing.JScrollPane classListScrollPane;
    private javax.swing.JButton sendButton;
    private javax.swing.JList userJList;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the chatTextField
     */
    public javax.swing.JTextField getChatTextField() {
        return chatTextField;
    }


}
